package entity.model.relationships;

import entity.model.Entity;
import misc.Title;

import java.lang.reflect.Field;

public class ManyToMany extends Relationship<entity.model.annotations.ManyToMany>{

    private final Entity pivot;

    public ManyToMany(Entity owner, Entity possession, Field field, Entity pivot) {
        super(owner, possession, field, entity.model.annotations.ManyToMany.class);
        this.pivot = pivot;
    }

    @Override
    protected String createPossessionPropertyName() {
        return annotation.hasMany().length() == 0 ? possession.getTitle().pluralize().toUpperCamel() : annotation.hasMany();
    }

    @Override
    protected String createOwnerPropertyName() {
        return Title.fromLowerCamel(field.getName()).pluralize().toLowerCamel();
    }

    public Entity getPivot() {
        return pivot;
    }

    @Override
    public String toString() {
        return String.format("%s (%s) %s, %s",
                super.toString(),
                pivot.getTablename(),
                owner.getTitle().toUpperCamel(),
                possession.getTitle().toUpperCamel()
        );
    }
}
